/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import CRUD.ForumHome;
import CRUD.ThreadHome;
import ModeratorOperations.ModeratorManageThreads;
import Utility.Utility;
import beans.MoveActionForm;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import xmlMapping.Thread;
import xmlMapping.Category;
import xmlMapping.Forum;
/**
 *
 * @author Mostafa
 */
public class MoveThreadAction extends DispatchAction {

 
    public ActionForward prepareMove(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        Integer id= Integer.parseInt(request.getParameter("threadid"));
        Thread movethread= new ThreadHome().findById(id);
        MoveActionForm bean=new MoveActionForm();
       List<Forum> forums=new LinkedList<Forum>();
       Utility u=new Utility();
       List<Category> categories= u.getCategories();
       Iterator<Category> itr=categories.iterator();
        while (itr.hasNext()) {
            Category category = itr.next();
           forums.addAll(category.getForums());             
           
        }
        forums.remove(movethread.getForum());
        bean.setForums(forums);
        bean.setTitle(movethread.getTitle());
        bean.setThreadid(id);
        
        request.setAttribute("MoveActionForm", bean);
        return mapping.findForward("movepage");
    }

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction2,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    public ActionForward Move(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
         Integer id= Integer.parseInt(request.getParameter("threadid"));
        Thread movethread= new ThreadHome().findById(id);
        String fname=movethread.getForum().getName();
        Integer fid=movethread.getForum().getForumId();
       
        MoveActionForm moveform=(MoveActionForm)form;
        movethread.setForum(new ForumHome().findById(moveform.getSelectedforum()));
        ModeratorManageThreads management=new ModeratorManageThreads();
        management.moveThread(movethread);
        
        
         response.sendRedirect("ViewForum.jsp?forumid="+fid+"&forumname="+fname);
        return null;
    }
}
